package com.frogs.ucenter.service.daoimpl;

import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.exception.BusinessRuntimeException;
import com.frogs.framework.util.PageList;
import com.frogs.ucenter.dao.ShoppingCartDao;
import com.frogs.ucenter.entity.ShoppingCart;
import com.frogs.ucenter.service.ShoppingCartService;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("shoppingCartService")
public class ShoppingCartServiceImpl extends BaseServiceImpl<ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;

    public ShoppingCartServiceImpl() {
        super();
    }

    public ShoppingCartDao getShoppingCartDao() {
        return shoppingCartDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<ShoppingCart> getBaseDao() {
        return shoppingCartDao;
    }

    public void setShoppingCartDao(ShoppingCartDao shoppingCartDao) {
        this.shoppingCartDao = shoppingCartDao;
    }

    public PageList<ShoppingCart> getAllParentList(Integer id) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqIdAsChildId", id);
        return getList( params, 1, 1000, false );
    }

    public PageList<ShoppingCart> getAllChildList(Integer id, int pageNum, int numPerPage) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqIdAsParentId", id);
        return getList( params, pageNum, numPerPage, true );
    }

    public PageList<ShoppingCart> getAllTopLevelList() {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqParentId", 0 );
        return getList( params, 1, 1000, false );
    }

    @Override
    public int delete(Object id) {
        Map<String,Object> params = new HashMap<String,Object>(); 
        params.put( "eqParentId", id );
        int count = shoppingCartDao.selectCountByMap( params );
        if( count > 0 ){
            throw new BusinessRuntimeException( "error.shoppingCart.has.childShoppingCart" );
        }
        return shoppingCartDao.deleteById( id );
    }

    @Override
    public int delete(Map<String, Object> param) {
        Map<String,Object> countparams = new HashMap<String,Object>(); 
        if( param.containsKey( "eqShoppingCartId" ) ){
            countparams.put( "eqParentId", param.get( "eqShoppingCartId" ) );
        }
        if( param.containsKey( "inShoppingCartIds" ) ){
            countparams.put( "inParentIds", param.get( "inShoppingCartIds" ) );
        }
        if( param.containsKey( "inShoppingCartIdList" ) ){
            countparams.put( "inParentIdList", param.get( "inShoppingCartIdList" ) );
        }
        if( countparams.size() > 0 ){
            int count = shoppingCartDao.selectCountByMap( countparams );
            if( count > 0 ){
                throw new BusinessRuntimeException( "error.shoppingCart.has.childShoppingCart" );
            }
        }
        return shoppingCartDao.deleteByMap( param );
    }
}